/*
 * SpriteTest.cpp
 *
 *  Created on: 15/11/2011
 *      Author: Tales
 */

#include "SpriteTest.hpp"
#include "core/Scene.hpp"
#include "video/Screen.hpp"
#include "core/Game.hpp"
#include "video/Sprite.hpp"

namespace yep
{

SpriteTest SpriteTest::_instance;

SpriteTest::SpriteTest() :
		Application("SpriteTest", "Tests sprite")
{
}

Scene & SpriteTest::getFirstScene()
{
	class SpriteTestScene: public Scene
	{
		Sprite *_sprite;
		Vector2 _sPosition;
	public:
		void setup(Game &game)
		{
			_sprite = new Sprite("res/sprites/clorofila_frente2.png", 32, 64);
			_sprite->transparent(0,0);
			_sPosition = {100,100};
		}
		void cleanup(Game &game)
		{
			delete _sprite;
		}
		void logic(Game &game)
		{
			SDL_Event event;
			while (SDL_PollEvent(&event))
			{
				if (event.type == SDL_QUIT)
					game.exit();
				else if (event.type == SDL_KEYDOWN)
				{
					switch (event.key.keysym.sym)
					{
					//EXIT
					case SDLK_ESCAPE:
						game.exit();
						break;
					//MOVE
					case SDLK_UP:
						_sPosition.y -= 4;
						break;
					case SDLK_DOWN:
						_sPosition.y += 4;
						break;
					case SDLK_LEFT:
						_sPosition.x -= 4;
						break;
					case SDLK_RIGHT:
						_sPosition.x += 4;
						break;
					//Increase velocity
					case SDLK_KP_PLUS:
						_sprite->setVelocity(_sprite->getVelocity()*2);
						break;
					//Decrease velocity
					case SDLK_KP_MINUS:
						_sprite->setVelocity(_sprite->getVelocity()/2);
						break;
					//Toggle animation
					case SDLK_SPACE:
						if(_sprite->getVelocity())
							_sprite->setVelocity(0);
						else
							_sprite->setVelocity(1);
						break;
					default:
						break;
					}
				}
			}
		}
		void draw(Game &game)
		{
			Screen &screen = Screen::getInstance();
			screen.clear(Color::BLUE);
			screen.fillRectangle(Vector2::ZERO, Vector2(screen.getWidth(), 8), Color::RED);
			string title = "Sprite Test";
			screen.drawText(Vector2(screen.getWidth()/2-title.size()*4,0), Color::LIGHT_YELLOW, title);

			_sprite->draw(screen, _sPosition);
		}

	};
	_scene = new SpriteTestScene;
	return *_scene;
}

} /* namespace yep */
